import {
  response,
  errorHandle,
  url
} from './response'

const getToken = () => {
  return wx.getStorageSync('auth');
}

export const request = {
  login(data, successCallback, failCallback) {

    wx.request({
      url: `${url}/login`,
      method: 'post',
      header: {
        'x-user-agent': 'miniprogram'
      },
      data,
      success(res) {

        const token = res.header['x-auth-token'];
        wx.setStorageSync('auth', token);
        response(res, successCallback, failCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 采集记录
   */
  collectionRecord(data, successCallback) {
    wx.request({
      url: `${url}/collection_record/search`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  collectionRecordItem(id, successCallback, failCallback) {
    wx.request({
      url: `${url}/collection_record/${id}`,
      method: 'get',
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback, failCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 添加采集记录
   */
  collectionRecordAdd(data, successCallback, failCallback) {
    wx.request({
      url: `${url}/collection_record/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //坯体原料添加
  rawMaterialAdd(data, successCallback) {
    wx.request({
      url: `${url}/raw_material/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**查询坯体原料 */
  rawMaterialGet(data, successCallback) {
    wx.request({
      url: `${url}/raw_material/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },

  /**坯体球磨 */
  ballMillingAdd(data, successCallback) {
    wx.request({
      url: `${url}/ball_milling/${data.id? 'update':'add'}`,
      method: 'POST',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**坯体球磨列表 */
  ballMillingList(data, successCallback) {
    wx.request({
      url: `${url}/ball_milling/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**球体喷粉 */
  dustingAdd(data, successCallback) {
    wx.request({
      url: `${url}/dusting/${data.id? 'update':'add'}`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**球体喷粉列表 */
  dustingList(data, successCallback) {
    wx.request({
      url: `${url}/dusting/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**球体干燥 */
  dryAdd(data, successCallback) {
    wx.request({
      url: `${url}/dry/${data.id? 'update':'add'}`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**球体干燥 */
  dryList(data, successCallback) {
    wx.request({
      url: `${url}/dry/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**配方 */
  recipeList(data, successCallback) {
    wx.request({
      url: `${url}/recipe/list`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**釉料 */
  glazeList(data, successCallback) {
    wx.request({
      url: `${url}/glaze/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询坯体釉料数据详情
   */
  glazeData(id, successCallback) {
    wx.request({
      url: `${url}/glaze/search`,
      method: 'get',
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  glazeAdd(data, successCallback) {
    wx.request({
      url: `${url}/glaze/${data.id? 'update':'add'}`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 坯体烧成添加
   * @param {*} data 
   * @param {*} successCallback 
   */
  firingAdd(data, successCallback) {
    wx.request({
      url: `${url}/firing/${data.id? 'update':'add'}`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 坯体烧成数据
   * @param {*} data 
   * @param {*} successCallback 
   */
  firingList(data, successCallback) {
    wx.request({
      url: `${url}/firing/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 添加成品数据
   * @param {*} data 
   * @param {*} successCallback 
   */
  productAdd(data, successCallback) {
    wx.request({
      url: `${url}/finish_product/${data.id? 'update':'add'}`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询成品数据
   * @param {*} data 
   * @param {*} successCallback 
   */
  productList(data, successCallback) {
    wx.request({
      url: `${url}/finish_product/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 添加成型数据
   * @param {*} data 
   * @param {*} successCallback 
   */
  formingAdd(data, successCallback) {
    wx.request({
      url: `${url}/forming/${data.id? 'update':'add'}`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询成型数据
   * @param {*} data 
   * @param {*} successCallback 
   */
  formingList(data, successCallback) {
    wx.request({
      url: `${url}/forming/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询印花方式列表
   * @param {*} data 
   * @param {*} successCallback 
   */
  printingMethodList(data, successCallback) {
    wx.request({
      url: `${url}/printingMethod/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 添加成型数据
   * @param {*} data 
   * @param {*} successCallback 
   */
  printingMethodAdd(data, successCallback) {
    wx.request({
      url: `${url}/printingMethod/${data.id? 'update':'add'}`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //获取配方性能数据
  getPerformance(data, successCallback) {
    ///recipe/performance
    wx.request({
      url: `${url}/recipe/performance`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //删除配方
  recipeRemove(data, successCallback) {
    wx.request({
      url: `${url}/recipe/remove`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询配方详情
   * @param {*} data 
   * @param {*} successCallback 
   */
  recipeId(id, successCallback) {
    wx.request({
      url: `${url}/recipe/${id}`,
      method: 'get',
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },

  /**
   * 添加原料配方
   * @param {*} data 
   * @param {*} successCallback 
   */
  ingredientsAdd(data, successCallback) {
    wx.request({
      url: `${url}/ingredients/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },

  /**
   * 查询原料配方详情
   * @param {*} data 
   * @param {*} successCallback 
   */
  ingredientsDesc(id, successCallback) {
    wx.request({
      url: `${url}/ingredients/${id}`,
      method: 'get',
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 修改原料配方详情
   * @param {*} data 
   * @param {*} successCallback 
   */
  ingredientsUpdate(data, successCallback) {
    wx.request({
      url: `${url}/ingredients/update`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 修改原料配方状态
   * @param {*} data 
   * @param {*} successCallback 
   */
  ingredientsUpdateStatus(data, successCallback) {
    wx.request({
      url: `${url}/ingredients/update/status`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查看记录统计
   * @param {*} data 
   * @param {*} successCallback 
   */
  homeStatistics(data, successCallback, reject) {
    wx.request({
      url: `${url}/collection_record/statistics`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback, reject);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },

  /**
   * 查看配方统计
   * @param {*} data 
   * @param {*} successCallback 
   */
  recipeStatistics(data, successCallback, reject) {
    wx.request({
      url: `${url}/recipe/statistics`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback, reject);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查看原料统计
   * @param {*} data 
   * @param {*} successCallback 
   */
  materialStatistics(data, successCallback, reject) {
    wx.request({
      url: `${url}/ingredients/statistics`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback, reject);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 搜索配方
   * @param {*} data 
   * @param {*} successCallback 
   */
  recipeSearch(data, successCallback) {
    wx.request({
      url: `${url}/recipe/search`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //recipe/update
  recipeUpdate(data, successCallback) {
    wx.request({
      url: `${url}/recipe/update`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //配方状态
  recipeUpdateStatus(data, successCallback) {
    wx.request({
      url: `${url}/recipe/update/status`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //添加配方
  recipeAdd(data, successCallback) {
    wx.request({
      url: `${url}/recipe/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询原料成分数据列表
   * @param {*} data 
   * @param {*} successCallback 
   */
  ingredientsList(data, successCallback, reject) {
    wx.request({
      url: `${url}/ingredients/list`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback, reject);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
        reject && reject();
      }
    })
  },
  /**
   * 搜索原料成分数据列表
   * @param {*} data 
   * @param {*} successCallback 
   */
  ingredientsSearch(data, successCallback) {
    wx.request({
      url: `${url}/ingredients/search`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 删除原料成分数据列表
   * @param {*} data 
   * @param {*} successCallback 
   */
  ingredientsRemove(data, successCallback) {
    wx.request({
      url: `${url}/ingredients/remove`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  // 搜索用户
  userSearch(data, successCallback) {
    wx.request({
      url: `${url}/user/search`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  // 修改用户
  userUpdate(data, successCallback) {
    wx.request({
      url: `${url}/user/update`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  // 添加用户
  userAdd(data, successCallback) {
    wx.request({
      url: `${url}/user/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询因子
   * @param {*} data 
   * @param {*} successCallback 
   */
  getFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 搜索因子
   * @param {*} data 
   * @param {*} successCallback 
   */
  searchtFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/search`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 删除因子
   * @param {*} data 
   * @param {*} successCallback 
   */
  removeFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/remove`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询因子详情
   * @param {*} data 
   * @param {*} successCallback 
   */
  factorManage(id, successCallback) {
    wx.request({
      url: `${url}/factor_manage/${id}`,
      method: 'get',
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 添加因子模板
   * @param {*} data 
   * @param {*} successCallback 
   */
  addFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  // 修改自己密码
  userPasswordUpdate(data, successCallback) {
    wx.request({
      url: `${url}/user/update_password`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //管理员修改密码
  rootPasswordUpdate(data, successCallback) {
    wx.request({
      url: `${url}/user/root/update_password`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //管理员修改账号状态
  rootEnabledUpdate(data, successCallback) {
    wx.request({
      url: `${url}/user/update_enabled`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //获取单一配置项目
  getSingleSetting(data, successCallback, failCallback) {
    wx.request({
      url: `${url}/settings/get`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback, failCallback);
      },
      fail: (res) => {
        failCallback && failCallback();
        errorHandle(res.errMsg);
      }
    })
  },
  //删除账号/user/remove
  userRemove(data, successCallback) {
    wx.request({
      url: `${url}/user/remove`,
      method: 'POST',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //更改配置
  updateSingleSetting(data, successCallback) {
    wx.request({
      url: `${url}/settings/update`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
}